package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Auther:ws@itany.com
 * Date:20-9-17 下午1:56
 * Version:1.0
 * Description:
 */
@Controller
@RequestMapping("request")
public class RequestController {

    @RequestMapping("f1")
    public String f1(HttpServletRequest request){
        User user = new User(1, "adim", "1234");
        request.setAttribute("user",user);
        return "forward:f2";
    }

    @RequestMapping("f2")
    public void f2(HttpServletRequest request){
        Object user = request.getAttribute("user");
        System.out.print(user);
    }

    @RequestMapping("f3")
    public String f3(User user){
        System.out.print(user);
        user.setId(2);
        user.setUsername("xiaoming");
        user.setPassword("12345");
        return "scope";
    }

    @RequestMapping("f4")
    public String f4(@ModelAttribute User user){
        System.out.print(user);
        User user1 = new User(2, "222", "2222");
        return "scope";
    }
    @ModelAttribute("users")
    public List<User> f5(){
        User user = new User(1,"admin","123456");
        User user2 = new User(2,"alice","111111");
        User user3 = new User(3,"jack","jack");
        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        users.add(user2);
        users.add(user3);
        return users;
    }
}
